/*
 * $Id: LocationImportTemplateService.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.service;

import com.sci.logisticsmap.data.model.LocationImportTemplateModel;
import com.sci.logisticsmap.data.repository.LocationImportTemplateRepository;
import com.sci.logisticsmap.data.support.sorting.LocationImportTemplateModelSorting;
import com.sci.logisticsmap.service.support.ServiceException;
import com.sci.logisticsmap.service.model.LocationImportTemplateLightDTModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * The Service for working with Location import template model.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
@Service
@Transactional
public class LocationImportTemplateService extends BaseService {
    /**
     * The Location import template repository.
     */
    @Autowired
    private LocationImportTemplateRepository locationImportTemplateRepository;

    /**
     * Sets the primary flag to <code>true</code>.
     *
     * @param id The Location import template id.
     *
     * @return <code>true</code> if the primary flag was changed, otherwise
     * <code>false</code>.
     */
    public boolean setPrimary(String id) {
        try {
            LocationImportTemplateModel locationImportTemplate;
            List<LocationImportTemplateModel> locationImportTemplatesToSave = new ArrayList<LocationImportTemplateModel>();

            locationImportTemplate = locationImportTemplateRepository.findPrimary();
            if (locationImportTemplate != null && !locationImportTemplate.getId().equals(id)) {
                locationImportTemplate.setPrimary(false);
                locationImportTemplatesToSave.add(locationImportTemplate);

                locationImportTemplate = locationImportTemplateRepository.findOne(id);
                if (locationImportTemplate != null) {
                    locationImportTemplate.setPrimary(true);
                    locationImportTemplatesToSave.add(locationImportTemplate);
                }

                locationImportTemplateRepository.save(locationImportTemplatesToSave);

                return true;
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
        return false;
    }

    /**
     * Retrieves the Location import template light models.
     *
     * @return The Location import template light models.
     */
    public List<LocationImportTemplateLightDTModel> getLightDTModels() {
        try {
            List<LocationImportTemplateModel> locationImportTemplates = locationImportTemplateRepository.findAll(LocationImportTemplateModelSorting.BY_NAME);
            if (locationImportTemplates != null) {
                return conversionUtilities.convert(locationImportTemplates, LocationImportTemplateLightDTModel.class);
            }
            return null;
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
}
